package com.heba.adapter.diary;

import android.content.Context;
import androidx.viewpager.widget.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.heba.bean.diary.ItemDiaryImageObject;
import com.heba.utils.GlideUtil;

import java.util.List;

/**
 * 日记列表图片adapter
 * <p>
 * Created by xingbao on 2019/2/12.
 */

public class ItemDiaryPagerAdapter extends PagerAdapter {

    private Context context;
    private List<ItemDiaryImageObject> list;

    public ItemDiaryPagerAdapter(Context context, List<ItemDiaryImageObject> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == arg1;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if (!list.isEmpty()) {
            ItemDiaryImageObject object = list.get(position);
            GlideUtil.loadImageWithDefaultPlaceHolder(context, object.getImageUrl(), imageView);
        }
        container.addView(imageView, 0);
        return imageView;
    }
}

